package com.kky.pp.callback

import android.util.Log
import android.view.MotionEvent
import android.view.View
import com.kky.pp.view.cardstackview.CardStackLayoutManager

fun View.homeCardClickListener(
    manager: CardStackLayoutManager,
    event: (eventEnum: EventEnum) -> Unit
) {
    this.setOnTouchListener(object : View.OnTouchListener {
        var x: Int = 0
        var y: Int = 0
        var nowX: Int = 0
        var nowY: Int = 0
        override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
            when (p1?.action) {
                MotionEvent.ACTION_DOWN -> {
                    x = p1.x.toInt()
                    y = p1.y.toInt()
                }

                MotionEvent.ACTION_MOVE -> {
                    nowX = p1.x.toInt()
                    nowY = p1.y.toInt()
                    if (y - nowY > 20) {
                        manager.isProhibitSliding = false
//                        userBanner.viewPager2.isUserInputEnabled = false
                    }
                }

                MotionEvent.ACTION_UP -> {
                    if (y - nowY > 150 && Math.abs(nowX - x) < 50) {
                        //上滑事件
                        event.invoke(EventEnum.UP_SCROLL)
                    }
                    if (Math.abs(nowX - x) < 5 && Math.abs(nowY - y) < 5) {
                        //点击事件
                        event.invoke(EventEnum.CLICK)
                    }
                    Log.d("Click", "nowY：" + nowY + "  y:" + y)

                    manager.isProhibitSliding = true
//                    userBanner.viewPager2.isUserInputEnabled = true
                }
            }
            return true
        }

    })
}

enum class EventEnum {
    UP_SCROLL, CLICK
}